<?php
namespace app\mouth\model;
use app\mouth\common\Encapsulation,
    think\Db;
class OrderModer extends Encapsulation{
    public function trailerOrder($table,$or_ordersn){

    }
    public function rescue($value){
       $date = $this->add($value);
       $date || exit(false);
       $this->insert('trailer',$date);
       return $date;
    }
    public static function index($date,$or_id){
        if ($or_id == ''){
            return Db::name('order')->insertGetId($date);
        }
    }

    public function manufacture($openid,$state,$full,$quanziid){
       $moceli = $this->one('wx_user',['openid'=>$openid],'mobile');
       if ($moceli['mobile'] == ''){
            return '请先完善手机号';
       }
       $where = array(
           'user_id'=>$quanziid,
           'state'=>$state,
       );

    $volume['id'] = $this->column('wx_user_discount',$where,'*','volume_id');
    if ( $volume['id'] ) {
        $res['discount'] = Db::name('discount')->where('full' , '<' ,$full)->where($volume)->where('die_time','>',time())->select();
        $res['mobile'] = $moceli['mobile'];
        return $res;
    } else{
        return $moceli;
        }
    }
    public function indexpay($value,$openid,$nickname){
        $id = Db::name('car_volume_store')->where('vs_id',$value['od_goodsid'])->field('vs_surplus')->find();
        if ($id['vs_surplus'] == 0){
            return '库存不足';
        }
        $date['ma_id']              = $value['ma_id'];//商铺id
        $date['or_openid']          = $openid;//用户openid
        $date['u_di_id']            = intval($value['di_id']);//优惠券id
        $date['mobile']             = $value['mobile'];//手机号
        $date['discount']           = intval($value['discount']);//优惠金额
        $date['nickname']           = $nickname;//昵称
        $date['ma_name']            = $value['ma_name'];//店铺名称
        $date['number']             = $value['number'];//购买数量
        $date['tradename']          = $value['tradename'];//商品名称
        $date['vs_type']            = $value['vs_type'];//类别
        $date['vs_picture']         = $value['vs_picture'];//商品图片
        $date['price']              = $value['price'];//单价
        $date['od_goodsid']         = $value['od_goodsid'];//商品id
        $date['s_time']             = time();//生成订单时间
        $date['or_status']          = 1;//生成订单时间
        //订单号
        $date['or_ordersn']         = date('Ymd').substr(implode(NULL,array_map('ord',str_split( substr(uniqid(), 7, 13) , 1))), 0, 8);
        //实际付款金额
        $date['or_goodsprice']      = actualpayment($date['number'],$date['price'],$date['discount']);
        if ($date['or_goodsprice'] < 0){
            return '添加失败';
        }
        $order = $this->insert('order',$date);
        if ($order){
            return $date;
        }else{
            return '添加失败';
        }
    }

    /**
     * @param $table
     * @param $where
     * @param $field
     * @param $number
     * @return array|false|mixed|\PDOStatement|string|\think\Model|null
     * 1  查询
     * 2  更新
     * 3  查询多条数据
     */
    public function aggregate($table,$where,$field,$number){
        switch ($number) {
            case "1":
                return $this->one($table,$where,$field);
                break;
            case "2":
                return $this->up($table,$where,$field);
                break;
            case "3":
                return $this->ones($table,$where,$field);
                break;
            case "4":
                return $this->setInc($table,$where,$field);
                break;
            default:
                $this->setDec($table,$where,$field);
        }

    }

    public function add($value){
        foreach ($value as $k=>$v){
            if (empty($v)){
                return false;
            }
            $date[$k] = $v;
        }
        if (!empty($date['rescue_distance']) && $date['rescue_distance'] > 15){
           $date['price'] = bcadd(bcmul(ceil($date['rescue_distance'] - 15),8),$date['price']);
        }
        $date['or_ordersn'] = 'S'.date('Ymd').substr(implode(NULL,array_map('ord',str_split( substr(uniqid(), 7, 13) , 1))), 0, 8);
        $date['time']= time();
        return $date;
    }
}


function actualpayment($number,$price,$discount=''){
    return bcsub(bcmul($number,$price,2),$discount,2);
}